// pages/shopbag/shopbag.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginFlag: false,
    tokenString: "",
    shopBag: "",
    count: "",
    index: "",
    countPrice: "",
    checkFlag: false,
    check: "",
    shopBagYN:false
  },

  //勾选框
  clickCheckbox(e) {
    let check = e.detail;
    this.setData({
      check: e.detail,
    });
    //勾选上以后便计算总价
    if (check[0] !== undefined) {
      this.countPrice();
    }
    //未勾选价格为0
    else {
      this.setData({
        countPrice: 0
      })
    }
  },

  //获取需要更改数量的商品
  onChange(e) {
    let count = e.detail;
    let index = e.currentTarget.dataset.text;
    this.setData({
      count: count,
      index: index
    })
    this.fix();
  },

  //提交订单按钮点击事件
  onClickButton() {
    // 检查是否有选中的商品
    if (!this.data.check || this.data.check.length === 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      });
      return;
    }

    // 获取选中商品的sid
    const selectedSids = [];
    const check = this.data.check;
    const shopBag = this.data.shopBag;
    
    for (let i = 0; i < check.length; i++) {
      selectedSids.push(shopBag[check[i]].sid);
    }

    // 跳转到结算页面，并传递选中的商品sid
    wx.navigateTo({
      url: '../checkout/checkout?sids=' + JSON.stringify(selectedSids)
    });
  },

  //修改商品数量
  fix() {
    let count = this.data.count;
    let index = this.data.index;
    wx.request({
      url: 'https://kf.webxyq.com/modifyShopcartCount',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      data: {
        appkey: app.globalData.appkey,
        tokenString: this.data.tokenString,
        sid: this.data.shopBag[index].sid,
        count: count
      },
      success: res => {
        this.onShow();
      }
    })
  },

  //计算总价
  countPrice() {
    let shopBag = this.data.shopBag;
    let countPrice = 0;
    let check = this.data.check;
    for (let i = 0; i < check.length; i++) {
      countPrice = countPrice + (shopBag[check[i]].count * shopBag[check[i]].price * 100)
    }
    this.setData({
      countPrice: countPrice
    })
  },

  //删除商品
  onDeleteItem(e) {
    let index = e.currentTarget.dataset.text;
    wx.request({
      url: 'https://kf.webxyq.com/deleteShopcart',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      data: {
        appkey: app.globalData.appkey,
        tokenString: this.data.tokenString,
        sids: JSON.stringify([this.data.shopBag[index].sid])
      },
      //删除成功
      success:res=>{
        this.onShow();
        if (this.data.shopBag.length == 0) {
          this.setData({
            shopBagYN:true
          })
          this.onShow();
        }
      }
    })
  },

  login() {
    wx.navigateTo({
      url: '../login/login',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    wx.getStorage({
      key: "_token",
      //已登陆
      success: res => {
        this.setData({
          tokenString: res.data,
          loginFlag: true
        })
        //查询购物车商品
        wx.request({
          url: 'https://kf.webxyq.com/findAllShopcart',
          method: 'GET',
          header: {
            "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
          },
          data: {
            appkey: app.globalData.appkey,
            tokenString: res.data
          },
          success: res => {
            this.setData({
              shopBag: res.data.result,
            })
            if (this.data.shopBag.length == 0) {
              this.setData({
                shopBagYN:true
              })
            }
            else{
              this.setData({
                shopBagYN:false
              })
            }
            this.countPrice();
          }
        })
      },
      //未登录
      fail: err => {
        this.setData({
          loginFlag: false
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})